/**
 * 
 */
package com.pactera.android.activity;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.pactera.android.R;
import com.pactera.android.adapter.ContactListAdapter;
import com.pactera.android.business.ContactListBusiness;
import com.pactera.android.db.ContactListDB;

/**
 * @author David Zheng
 *
 */
public class ContactListActivity extends Activity {	
	
	static final String TAG = "ContactListActivity";
	
	private ContactListDB contactListDB;
	
	private ListView contactListView;
	
	private ContactListAdapter adapter;
	
	private ContactListBusiness business;
	
	private LoadDataTask task;


	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "ListActivity");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_list);
		contactListView = (ListView)findViewById(R.id.list);

		contactListDB = new ContactListDB(this);
		contactListDB.OpenDB();
		
		business = new ContactListBusiness(contactListDB);
		
		task = new LoadDataTask();
		task.execute("load");
	}

//	/* (non-Javadoc)
//	 * @see android.app.Activity#onResume()
//	 */
//	@SuppressWarnings("deprecation")
//	@Override
//	protected void onResume() {
//		super.onResume();
//		cursor = contactListDB.query(Constants.CONTACT_LIST_TABLE, null, null, null, null, null, null);
//		startManagingCursor(cursor);
//		
//		adapter = new ContactListAdapter(this, cursor);
//		contactListView.setAdapter(adapter);
//	}
	
		
	/* (non-Javadoc)
	 * @see android.app.ListActivity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		contactListDB.CloseDb();
	}

	
	public class LoadDataTask extends AsyncTask<String, Integer, Boolean> {

		@Override
		protected Boolean doInBackground(String... cmd) {
			// TODO Auto-generated method stub
			Cursor c;
			if(cmd[0].equalsIgnoreCase("sync")){
				boolean ret = business.syncData();				
				if(!ret){					
					return false;
				}				
			}
			
			c = business.loadData();
			if(c!=null && c.getCount()>0){
				return true;
			}
			else{
				return false;			
			}
		}

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();		
		}
		
		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);
			//contactListView.updateProgressBar(values[0]);
		}
		

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if(result){	
				updateContacts(business.getCursor());
				
				Toast.makeText(ContactListActivity.this, "Update success", Toast.LENGTH_SHORT).show();
			}
			else{
				Toast.makeText(ContactListActivity.this, "Update failed", Toast.LENGTH_SHORT).show();
			}
			
		}

	}
	
	private void updateContacts(Cursor c){
		//Update the list by contact cursor

		if(adapter == null){
			adapter = new ContactListAdapter(this, c);
			
			contactListView.setAdapter(adapter);
		}
		else{
			adapter.changeCursor(c);
			adapter.notifyDataSetChanged();
		}
		
	}
	

}
